package com.lingnan.tijian.handler;

import com.lingnan.tijian.dto.ApiResponse;
import com.lingnan.tijian.exception.TijianAPPExceptionEnum;
import com.lingnan.tijian.exception.TijianAppException;
import org.apache.commons.logging.LogFactory;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;



@ControllerAdvice
public class GlobalExceptionHandler {



    //只处理业务异常
    @ExceptionHandler(TijianAppException.class)
    @ResponseBody  //返回json格式数据给前端
    public ApiResponse handleTijianAppException(TijianAppException e){
        e.printStackTrace();
        System.out.println("异常信息："+e.getStackTrace());

        return new ApiResponse(e.getCode(),e.getMessage());

    }

    //处理所有异常
    @ExceptionHandler(Exception.class)
    @ResponseBody  //返回json格式数据给前端
    public ApiResponse handleAllException(Exception e){
e.printStackTrace();
        System.out.println("异常信息："+e.getStackTrace()+",message:"+e.getMessage());
        return new ApiResponse(TijianAPPExceptionEnum.SYSTEM_ERROR.getCode(),TijianAPPExceptionEnum.SYSTEM_ERROR.getMessage());

    }



}
